package com.redwood.report.activity.bak;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redwood.report.ItemDecoration.SpaceItemDecoration;
import com.redwood.report.R;
import com.redwood.report.adapter.bak.SheBeiAdapter;
import com.redwood.report.base.BaseActivity;
import com.redwood.report.common.Constant;
import com.redwood.report.entity.ServiceResult;
import com.redwood.report.entity.bak.SheBeiInfo;
import com.redwood.report.event.SheBeiChangeEvent;
import com.redwood.report.net.Api;
import com.redwood.report.net.NetUtils;
import com.redwood.report.util.ToastUtils;
import com.redwood.report.util.actionbar.LightStatusBarUtils;
import com.redwood.report.util.actionbar.ToolbarUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择设备
 * Created by Supeng on 2018/10/12.
 */

public class SelectedSheBeiActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.search_iv)
    Button searchIv;
    @BindView(R.id.search_ll)
    LinearLayout searchLl;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.root_layout)
    LinearLayout rootLayout;
    @BindView(R.id.tv_count)
    TextView tvCount;
    private BaseQuickAdapter homeAdapter;
    private int page = 1;
    private int totalpage = Integer.MAX_VALUE;
    private String key="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shebei_sel);
        unbinder = ButterKnife.bind(this);
        //标题栏颜色
        ToolbarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.blueColorAppKey));
        //深色主题 true字颜色为深色 false 为白色标题字
        LightStatusBarUtils.setLightStatusBar(this, false);
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        searchIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                key=searchEt.getText().toString().trim();
                page = 1;
                totalpage = Integer.MAX_VALUE;
                loadServer(false);
            }
        });
        initAdapter();
        loadServer(false);
    }

    private void initAdapter() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(1f).setSpaceColor(getResources().getColor(R.color.item_press2)));
        homeAdapter = new SheBeiAdapter();
        homeAdapter.setOnLoadMoreListener(this, rvList);
        homeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {

                }
            }
        });
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                EventBus.getDefault().post(new SheBeiChangeEvent((SheBeiInfo.DataBean) adapter.getData().get(position)));
                finish();
            }
        });
        rvList.setAdapter(homeAdapter);

    }

    @Override
    public void onRefresh() {
        page = 1;
        totalpage = Integer.MAX_VALUE;
        loadServer(false);
    }

    private void loadServer(final boolean isLoadMore) {
//        totalpage=1;
//        List<SheBeiInfo.DataBean> listBaoXiuBeens = new ArrayList();
//        for (int i = homeAdapter.getData().size(); i < homeAdapter.getData().size()+10; i++) {
//            SheBeiInfo.DataBean dataBean = new SheBeiInfo.DataBean();
//            dataBean.setMacName("立式加工中心");
//            dataBean.setModelNo("VM-3(四轴)");
//            dataBean.setMacID("M0000"+i);
//            dataBean.setNumber("M0000"+i);
//            listBaoXiuBeens.add(dataBean);
//        }
//        refreshLayout.setRefreshing(false);
//        if(page==1) {
//            homeAdapter.setNewData(listBaoXiuBeens);
//            page++;
//        }else{
//            homeAdapter.addData(listBaoXiuBeens);
//            page++;
//        }
//        setCount(homeAdapter.getData().size());
//


        Api.machine(getActivity(),key,page, Constant.PAGE_SIZE, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                refreshLayout.setRefreshing(false);
                SheBeiInfo info = (SheBeiInfo) rspData;
                if (rspData.getRecode() == 1) {
                    if (page == 1) {
                        homeAdapter.setNewData(info.getData());
                    } else {
                        homeAdapter.addData(info.getData());
                    }
                    homeAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvList.getParent());
                    totalpage = (info.getCount() + Constant.PAGE_SIZE -1) / Constant.PAGE_SIZE;
                    page++;
                    if (isLoadMore) {
                        refreshLayout.setEnabled(true);
                        homeAdapter.loadMoreComplete();
                    } else {
                        refreshLayout.setRefreshing(false);
                    }

                } else {
                    if (isLoadMore) {
                        refreshLayout.setEnabled(true);
                        homeAdapter.loadMoreFail();
                    } else {
                        refreshLayout.setRefreshing(false);
                    }
                    ToastUtils.show(rspData.getMsg());
                }
                setCount(info.getCount());
                homeAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvList.getParent());
            }

            @Override
            public void failed(String msg) {
                if (isLoadMore) {
                    refreshLayout.setEnabled(true);
                    homeAdapter.loadMoreFail();
                } else {
                    refreshLayout.setRefreshing(false);
                }
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, SheBeiInfo.class);
    }

    public void setCount(int count) {
        String counStr="共"+count+"条记录";
        SpannableStringBuilder style = new SpannableStringBuilder(counStr);
        style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.orange)), 1, (count+"").length()+1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //颜色
        tvCount.setText(style);
    }

    //加载更多
    @Override
    public void onLoadMoreRequested() {
        if(refreshLayout==null){
            return;
        }
        refreshLayout.setEnabled(false);
        if (page > totalpage) {
            refreshLayout.setEnabled(true);
            homeAdapter.loadMoreEnd(false);
            return;
        } else {
            if (homeAdapter.getData().size() < Constant.PAGE_SIZE) {
                homeAdapter.loadMoreEnd(true);
            } else {
                loadServer(true);
            }
        }
    }


}
